/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import io.renren.service.SysGeneratorService;
import io.renren.utils.GenUtils;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器
 * 
 * @author Mark sunlightcs@gmail.com
 */
@Controller
@RequestMapping("/sys/generator")
public class SysGeneratorController {
	@Autowired
	private SysGeneratorService sysGeneratorService;
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		System.out.println("11111");
		PageUtils pageUtil = sysGeneratorService.queryList(new Query(params));
		
		return R.ok().put("page", pageUtil);
	}

	@ResponseBody
	@RequestMapping("listTemplate")
	public R listTemplate(){
		List<Map<String, Object>> templates = new ArrayList<>();
        templates.add(Map.of("tempPath","template/Entity.java.vm","tempName","Entity.java"));
		templates.add(Map.of("tempPath","template/Dao.xml.vm","tempName","Dao.xml"));
        templates.add(Map.of("tempPath","template/Dao.java.vm","tempName","Dao.java"));
        templates.add(Map.of("tempPath","template/EntityDto.java.vm","tempName","EntityDto.java"));
        templates.add(Map.of("tempPath","template/Service.java.vm","tempName","Service.java"));
        templates.add(Map.of("tempPath","template/Manager.java.vm","tempName","Manager.java"));
        templates.add(Map.of("tempPath","template/ManagerImpl.java.vm","tempName","ManagerImpl.java"));
        templates.add(Map.of("tempPath","template/EntityReq.java.vm","tempName","EntityReq.java"));
        templates.add(Map.of("tempPath","template/EntityResp.java.vm","tempName","EntityResp.java"));
        templates.add(Map.of("tempPath","template/EntityCreateReq.java.vm","tempName","EntityCreateReq.java"));
        templates.add(Map.of("tempPath","template/EntityUpdateReq.java.vm","tempName","EntityUpdateReq.java"));

		List<Map<String, Object>> templates1 = new ArrayList<>();
        templates1.add(Map.of("tempPath","template/Controller.java.vm","tempName","Controller.java"));
        templates1.add(Map.of("tempPath","template/Service_Portal.java.vm","tempName","Service_Portal.java"));
        templates1.add(Map.of("tempPath","template/EntityCreateReq_Portal.java.vm","tempName","EntityCreateReq_Portal.java"));
        templates1.add(Map.of("tempPath","template/EntityResp_Portal.java.vm","tempName","EntityResp_Portal.java"));
        templates1.add(Map.of("tempPath","template/EntityUpdateReq_Portal.java.vm","tempName","EntityUpdateReq_Portal.java"));
        templates1.add(Map.of("tempPath","template/EntityDto_Portal.java.vm","tempName","EntityDto_Portal.java"));
        templates1.add(Map.of("tempPath","template/EntityReq_Portal.java.vm","tempName","EntityReq_Portal.java"));
        templates1.add(Map.of("tempPath","template/index.html.vm","tempName","index.html"));
		templates1.add(Map.of("tempPath","template/menu.sql.vm","tempName","menu.sql"));

		Map<String,Object> result = new HashMap<>();
		result.put("service",templates);
		result.put("front",templates1);
		return R.ok().put("data",result);
	}

	/**
	 * 生成代码
	 */
	@RequestMapping("/code")
	@ResponseBody
	public R code(@RequestBody CodeReq codeReq, HttpServletResponse response) throws IOException{
		sysGeneratorService.generatorCode(codeReq);
		return R.ok();
	}


	@ResponseBody
	@RequestMapping("/openDirectory")
	public R openDirectory(){
		try {
			Runtime.getRuntime().exec("explorer.exe /select, "+ GenUtils.getConfig().getString("path").replaceAll("/","\\\\")+"\\main\\");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return R.ok();
	}
}
